package world.criver.imagecompresstool.data;

import world.criver.imagecompresstool.function.funReg;

import java.io.*;

public class daConfig {
    /**
     * 开机自启
     */
    public static boolean bootstrap=true;
    /**
     * 取色
     */
    public static boolean color_picker=true;
    /**
     * 取色热键
     */
    public static String color_picker_hot_key="F1";
    /**
     * 上下文菜单
     */
    public static boolean content_menu=true;
    /**
     * 启动是否打开
     */
    public static boolean show_master=true;
    /**
     * 压缩模式
     */
    public static String mode_compress="compress1";
    /**
     * 转ico模式
     */
    public static String mode_ico="ico1";
    /**
     * 压缩替换原图
     */
    public static boolean compress_replace=false;

    /**
     * 加载配置文件
     */
    public static void load(){
        File f=new File("config.config");
        if(!f.exists())return;
        try(BufferedReader re=new BufferedReader(new FileReader("config.config"))){
            String s;
            while((s=re.readLine())!=null){
                if(s.startsWith("bootstrap="))bootstrap=Boolean.parseBoolean(s.substring(10));
                else if(s.startsWith("content_menu="))content_menu= Boolean.parseBoolean(s.substring(13));
                else if(s.startsWith("show_master="))show_master=Boolean.parseBoolean(s.substring(12));
                else if(s.startsWith("mode_compress="))mode_compress=s.substring(14);
                else if(s.startsWith("mode_ico="))mode_ico=s.substring(9);
                else if(s.startsWith("compress_replace="))compress_replace=Boolean.parseBoolean(s.substring(17));
                else if(s.startsWith("color_picker="))color_picker=Boolean.parseBoolean(s.substring(13));
                else if(s.startsWith("color_picker_hot_key="))color_picker_hot_key=s.substring(21);
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    /**
     * 保存配置文件
     */
    public static void save(){
        try(BufferedWriter wr=new BufferedWriter(new FileWriter("config.config"))){
            wr.write("bootstrap="+bootstrap+"\n");
            wr.write("content_menu="+content_menu+"\n");
            wr.write("show_master="+show_master+"\n");
            wr.write("mode_compress="+mode_compress+"\n");
            wr.write("mode_ico="+mode_ico+"\n");
            wr.write("compress_replace="+compress_replace+"\n");
            wr.write("color_picker="+color_picker+"\n");
            wr.write("color_picker_hot_key="+color_picker_hot_key);
            new funReg().change();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}
